package com.longxi.tianjintianbao.ui.presenter.quality;

import android.content.Intent;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SelectQualityItemListener;
import com.longxi.tianjintianbao.model.quality.QualityItem;
import com.longxi.tianjintianbao.ui.activity.quality.QualityScoreActivity;
import com.longxi.tianjintianbao.ui.activity.quality.QualityStateChangesActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.QualityItemViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.quality.IQualityItemAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;
import com.longxi.tianjintianbao.utils.TimeComparisonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class QualityItemAtPresenter extends BasePresenter<IQualityItemAtView> {

    private String classname = "";
    private String classid = "";
    private String billid = "";

    private String isfinish = "0"; //0否 1是 是否完成
    private String isfinished = "0"; //0否 1是 是否已生成
    private String needmodify = "0"; //0否 1是 是否需要整改
    private String build = ""; //0否 1是 是否已生成
    private String buildid = ""; //0否 1是 是否需要整改

    private MultiTypeAdapter adapter;
    private Items items = new Items();

    public QualityItemAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            SelectQualityItemListener listener = new SelectQualityItemListener() {
                @Override
                public void onSuccess(QualityItem qualityItem) {
                    Intent mainIntent = new Intent(mContext, QualityScoreActivity.class);
                    mainIntent.putExtra(AppConst.QUALITYITEM, qualityItem);
                    mainIntent.putExtra(AppConst.BILLID, billid);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_QUALITY_SCORE);
                }
            };
            /* 注册类型和 View 的对应关系 */
            adapter.register(QualityItem.class, new QualityItemViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        classname =intent.getStringExtra(AppConst.CLASSNAME);
        classid =intent.getStringExtra(AppConst.CLASSID);
        billid =intent.getStringExtra(AppConst.BILLID);
        mContext.setTitle(classname);
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }


    public void getQualityItem() {
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
//            object.put("classid", classid);
            object.put("billid", billid);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getQualityItem(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<QualityItem>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<QualityItem> result) {

                for (QualityItem item : result.getData()) {
                    if(item.getIsfinish().equals("1")){
                        isfinish = "1";
                    }
                    if(item.getIsfinshed().equals("1")){
                        isfinished = "1";
                    }
                    if(item.getNeedmodify().equals("1")){
                        needmodify = "1";
                    }

                    build = item.getBuild();
                    buildid = item.getBuildid();
                }
                showBtn();

                items.clear();
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    private void showBtn() {
        mContext.setResult(mContext.RESULT_OK);
        getView().getBtnLogin().setVisibility(View.GONE);
        if(isfinish.equals("0")){
            getView().getBtnLogin().setVisibility(View.VISIBLE);
            getView().getBtnLogin().setText("完成");
            return;
        }

        if(isfinished.equals("1")){
            getView().getBtnLogin().setVisibility(View.GONE);
            return;
        }

        if(needmodify.equals("1")){
            getView().getBtnLogin().setVisibility(View.VISIBLE);
            getView().getBtnLogin().setText("生成整改单");
            return;
        }
    }

    public void goFinshOrChange() {
        Intent intent = new Intent(mContext, QualityStateChangesActivity.class);
        intent.putExtra(AppConst.BILLID, billid);
        intent.putExtra(AppConst.BUILD, build);
        intent.putExtra(AppConst.BUILDID, buildid);
        if(isfinish.equals("0")){
            //结束
            intent.putExtra(AppConst.STATE, "1");
            mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_QUALITY_STATE_CHANGES);
            return;
        }

        if(needmodify.equals("1")){
            //生成整改单
            intent.putExtra(AppConst.STATE, "2");
            mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_QUALITY_STATE_CHANGES);
            return;
        }
    }

}
